package com.xxg.renrenproduct.controller;

import com.xxg.renrencommon.domain.bean.Product;
import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrenproduct.domain.query.AddProductCollectionQuery;
import com.xxg.renrenproduct.domain.query.GetCollectionListQuery;
import com.xxg.renrenproduct.service.ProductCollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商品收藏管理
 *
 * @author szj
 * @date 2021/11/29
 */
@Api(tags = "商品收藏管理")
@RestController
@RequestMapping("/productCollection")
@AllArgsConstructor
public class ProductCollectionController {
    private final ProductCollectionService productCollectionService;

    @ApiOperation("添加商品到收藏")
    @RequestMapping(value = "add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "添加商品到收藏参数", required = true, dataType = "AddProductCollectionQuery")})
    public BaseResult<Void> add(@RequestBody @Validated AddProductCollectionQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return productCollectionService.add(userId, query);
    }

    @ApiOperation("取消商品收藏")
    @RequestMapping(value = "cancel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "添加商品到收藏参数", required = true, dataType = "AddProductCollectionQuery")})
    public BaseResult<Void> cancel(@RequestBody @Validated AddProductCollectionQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return productCollectionService.cancel(userId, query);
    }

    @ApiOperation("查询收藏")
    @RequestMapping(value = "list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "分页获取收藏列表参数", required = true, dataType = "GetCollectionListQuery")})
    public BaseResult<List<Product>> list(@RequestBody @Validated GetCollectionListQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return productCollectionService.list(userId, query);
    }

    @ApiOperation("根据spu查询收藏")
    @RequestMapping(value = "listBySpuId", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "spuId", value = "spuId", required = true, dataType = "Integer", paramType = "query")})
    public BaseResult<List<String>> listBySpuId(@RequestParam("spuId") Integer spuId, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return productCollectionService.listBySpuId(userId, spuId);
    }
}
